import axios from "axios";
import { useState, useEffect } from "react";

const useHots = () => {
    const [type, setType] = useState("baidu");
    const [hots, setHots] = useState([]);

    // 获取热搜数据
    const getHots = async () => {
        const res = await axios.get(
            `https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=${type}`
        );
        setHots(res.data.result.slice(0, 4));
    };

    // 监听type的变化
    useEffect(() => {
        getHots();
    }, [type]);

    // 返回热搜数据和改变type的方法
    return { hots, type, setType };
};

export default useHots;
